//
//  UIFont+Normal.m
//  RNApps
//
//  Created by yao zhaofeng on 2018/5/30.
//  Copyright © 2018年 JJ. All rights reserved.
//

#import "UIFont+Normal.h"
#import <objc/runtime.h>

@implementation UIFont (Normal)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = object_getClass(self);
        SEL originalSelector = @selector(systemFontOfSize:);
        SEL swizzleSelector = @selector(xy_systemFontOfSize:);

        Method originalMethod = class_getClassMethod(class, originalSelector);
        Method swizzleMethod = class_getClassMethod(class, swizzleSelector);
        
        BOOL result = class_addMethod(class, originalSelector, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod));
        if (result) {
            class_replaceMethod(class, swizzleSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzleMethod);
        }

        originalSelector = @selector(boldSystemFontOfSize:);
        swizzleSelector = @selector(xy_boldSystemFontOfSize:);

        originalMethod = class_getClassMethod(class, originalSelector);
        swizzleMethod = class_getClassMethod(class, swizzleSelector);
        
        result = class_addMethod(class, originalSelector, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod));
        if (result) {
            class_replaceMethod(class, swizzleSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzleMethod);
        }
    });
}

+ (UIFont *)xy_systemFontOfSize:(CGFloat)fontSize {
    UIFont *font = [UIFont fontWithName:@"LTHYSZK" size:fontSize];
    if (!font) {
        font = [UIFont xy_systemFontOfSize:fontSize];
    }
    return font;
}

+ (UIFont *)xy_boldSystemFontOfSize:(CGFloat)fontSize {
    //自定义字体没有加粗字体
    UIFont *font = [UIFont fontWithName:@"LTHYSZK" size:fontSize];
    if (!font) {
        font = [UIFont xy_boldSystemFontOfSize:fontSize];
    }
    return font;
}

@end
